package org.ponly.cta4j.factory.gimpy;

import com.jhlabs.image.SwimFilter;
import com.jhlabs.math.ImageFunction2D;
import org.ponly.cta4j.CaptchaFactory;
import org.ponly.cta4j.drawing.FilterablePainter;
import org.ponly.cta4j.drawing.Painter;
import org.ponly.cta4j.drawing.background.BackgroundBrush;
import org.ponly.cta4j.drawing.background.ColorBackgroundBrush;
import org.ponly.cta4j.drawing.color.SingleColorGenerator;
import org.ponly.cta4j.drawing.filter.BufferedImageOpFilter;
import org.ponly.cta4j.drawing.filter.Filter;
import org.ponly.cta4j.drawing.font.FontFamily;
import org.ponly.cta4j.drawing.font.RandomFontFamily;
import org.ponly.cta4j.drawing.text.GlyphsBrush;
import org.ponly.cta4j.drawing.text.TextBrush;
import org.ponly.cta4j.drawing.text.glyphsvisitor.GlyphsVisitor;
import org.ponly.cta4j.drawing.text.glyphsvisitor.OverlapGlyphsUsingShapeVisitor;
import org.ponly.cta4j.drawing.text.glyphsvisitor.TranslateAllToRandomPointVisitor;
import org.ponly.cta4j.factory.ProxiedCaptchaFactory;

import java.awt.*;

/**
 * Hotmail 2008 风格的验证码工厂, 来自于网络 jcaptcha HotmailEngine2008.
 *
 * @author vacoor
 * @since 1.0
 */
public class Hotmail2008Factory extends ProxiedCaptchaFactory {
    /**
     * 默认实例.
     */
    public static final Hotmail2008Factory INSTANCE = new Hotmail2008Factory();

    /**
     * Hotmail captcha width.
     */
    private static final int HOTMAIL_WIDTH = 218;

    /**
     * Hotmail captcha height.
     */
    private static final int HOTMAIL_HEIGHT = 48;

    private final GimpyFactory hotmail2008Factory = createHotmail2008Factory();

    /**
     * {@inheritDoc}
     */
    @Override
    protected CaptchaFactory getCaptchaFactory() {
        return hotmail2008Factory;
    }

    /**
     * 创建一个 HOTMAIL 2008 风格的验证码工厂.
     *
     * @return hotmail style 验证码工程
     */
    protected GimpyFactory createHotmail2008Factory() {
        final SwimFilter swim = new SwimFilter();
        swim.setScale(30);
        swim.setAmount(10);
        swim.setEdgeAction(ImageFunction2D.CLAMP);

        final SwimFilter swim2 = new SwimFilter();
        swim2.setScale(30);
        swim2.setAmount(10);
        swim2.setTime(90);
        swim2.setEdgeAction(ImageFunction2D.CLAMP);

        final Filter textFilter = BufferedImageOpFilter.create(swim, swim2);
        final BackgroundBrush backgroundBrush = new ColorBackgroundBrush(new Color(238, 238, 238));
        final TextBrush textBrush = new GlyphsBrush(8, 8, new SingleColorGenerator(
                new Color(0, 0, 80)
        ), new GlyphsVisitor[]{
                new OverlapGlyphsUsingShapeVisitor(3),
                new TranslateAllToRandomPointVisitor(20, 20)
        });
        final FontFamily fontFamily = new RandomFontFamily(30, 35, new Font[]{
                new Font("Caslon", Font.BOLD, 30)
        }, false);

        final Painter painter = new FilterablePainter(backgroundBrush, textBrush, fontFamily, textFilter);
        final RandomTextGenerator gen = new RandomTextGenerator("ABCDEGHJKLMNRSTUWXY235689");

        return new GimpyFactory(HOTMAIL_WIDTH, HOTMAIL_HEIGHT, painter, gen, false);
    }
}
